home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / Onboard / KeyCommon.py < prev    next >
Text File  |  2009-10-01  |  8KB  |  256 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. from math import sqrt
  4.  
  5. ### Logging ###
  6. import logging
  7. _logger = logging.getLogger("KeyCommon")
  8. ###############
  9.  
  10. BASE_PANE_TAB_HEIGHT = 40
  11.  
  12. (CHAR_ACTION, KEYSYM_ACTION, KEYCODE_ACTION, MODIFIER_ACTION, MACRO_ACTION,
  13.     SCRIPT_ACTION, KEYPRESS_NAME_ACTION) = range(1,8)
  14.  
  15. # KeyCommon hosts the abstract classes for the various types of Keys.
  16. # UI-specific keys should be defined in KeyGtk or KeyKDE files.
  17. # NOTE: I really don't like the way pointWithinKey() is handled.
  18. # I won't change it now, but we should strive for maximum
  19. # efficency of inheritance (move the poinWithinKey() to
  20. # the Key class and only tweak it for the other classes.
  21.  
  22.  
  23. class KeyCommon:
  24.     """ a library-independent key class. Specific 
  25.         rendering options are stored elsewhere. """
  26.  
  27.     action_type = None
  28.     """Type of action to do when key is pressed."""
  29.  
  30.     action = None
  31.     """Data used in action."""
  32.  
  33.     on = False
  34.     """True when key is being pressed."""
  35.  
  36.     stuckOn = False
  37.     """When key is sticky and pressed twice."""
  38.  
  39.     sticky = False
  40.     """Keys that stay stuck when pressed like modifiers."""
  41.  
  42.     beingScanned = False
  43.     """True when onboard is in scanning mode and key is highlighted"""
  44.  
  45.     font_size = 1
  46.     """ Size to draw the label text in Pango units"""
  47.  
  48.     label_rgba = (0.0,0.0,0.0,1.0)
  49.     """ Four tuple with values between 0 and 1 containing label color"""
  50.  
  51.     label_index = 0
  52.     """ Index in labels that is currently displayed by this key """
  53.  
  54.     labels = None
  55.     """ Labels which are displayed by this key """
  56.  
  57.     label_offset = None
  58.     """ The amount to offset the label in each direction """
  59.  
  60. ###################
  61.  
  62.     def __init__(self):
  63.         pass
  64.  
  65.     def on_size_changed(self, scale):
  66.         raise NotImplementedError()
  67.  
  68.     def configure_label(self, mods, scale):
  69.         if mods[1]:
  70.             if mods[128] and self.labels[4]:
  71.                 self.label_index = 4
  72.             elif self.labels[2]:
  73.                 self.label_index = 2
  74.             elif self.labels[1]:
  75.                 self.label_index = 1
  76.             else:
  77.                 self.label_index = 0
  78.         
  79.         elif mods[128] and self.labels[3]:
  80.             self.label_index = 3
  81.         
  82.         elif mods[2]:
  83.             if self.labels[1]:
  84.                 self.label_index = 1
  85.             else:
  86.                 self.label_index = 0
  87.         else:
  88.             self.label_index = 0
  89.  
  90.     def paint_font(self, scale, location, context = None):
  91.         raise NotImplementedError()
  92.  
  93. class TabKeyCommon(KeyCommon):
  94.  
  95.     pane = None
  96.     """Pane that this key is on."""
  97.  
  98.     """ class for those tabs up the right hand side """
  99.     def __init__(self, keyboard, width, pane):
  100.         KeyCommon.__init__(self)
  101.         
  102.         self.pane = pane
  103.         self.width = width
  104.         self.keyboard = keyboard
  105.         self.modifier = None # what for?
  106.         self.sticky = True
  107.  
  108.     def pointWithinKey(self, widget, mouseX, mouseY):
  109.         """ does exactly what the name says - checks for the 
  110.             mouse within a key. returns bool. """
  111.         if (mouseX > self.keyboard.kbwidth 
  112.             and mouseY > self.height*self.index + BASE_PANE_TAB_HEIGHT 
  113.             and mouseY < self.height*(self.index + 1)+ BASE_PANE_TAB_HEIGHT):
  114.             return True
  115.         else:
  116.             return False
  117.  
  118.     def paint(self, context):
  119.         """ paints the TabKey object """
  120.         self.height = (self.keyboard.height / len(self.keyboard.panes)) - (BASE_PANE_TAB_HEIGHT / len(self.keyboard.panes))
  121.         self.index = self.keyboard.panes.index(self.pane)
  122.     
  123.     
  124. class BaseTabKeyCommon(KeyCommon):
  125.  
  126.     pane = None
  127.     """Pane that this key is on."""
  128.  
  129.     """ class for the tab that brings you to the base pane """
  130.     def __init__(self, keyboard, width):
  131.         KeyCommon.__init__(self)
  132.        
  133.         self.width = width
  134.         self.keyboard = keyboard
  135.         self.modifier = None # what for?
  136.         self.sticky = False
  137.  
  138.     def pointWithinKey(self, widget, mouseX, mouseY):
  139.         if (mouseX > self.keyboard.kbwidth 
  140.             and mouseY < BASE_PANE_TAB_HEIGHT):
  141.             return True
  142.         else:
  143.             return False
  144.  
  145.    
  146.     def paint(self,context=None):
  147.         """Don't draw anything for this key"""
  148.         pass
  149.  
  150. class LineKeyCommon(KeyCommon):
  151.     """ class for keyboard buttons made of lines """
  152.     
  153.     name = None
  154.     """ Unique identifier for the key """
  155.  
  156.     def __init__(self, name, pane, coordList, fontCoord, rgba):
  157.         KeyCommon.__init__(self, pane)
  158.         self.name = name
  159.         self.coordList = coordList
  160.         self.fontCoord = fontCoord
  161.         self.rgba = rgba
  162.         
  163.     def pointCrossesEdge(self, x, y, xp1, yp1, sMouseX, sMouseY):
  164.         """ Checks whether a point, when scanning from top left crosses edge"""
  165.         return ((((y <= sMouseY) and ( sMouseY < yp1)) or  
  166.             ((yp1 <= sMouseY) and (sMouseY < y))) and 
  167.             (sMouseX < (xp1 - x) * (sMouseY - y) / (yp1 - y) + x))
  168.         
  169.     
  170.     def point_within_key(self, location, scale):
  171.         """Checks whether point is within shape.
  172.            Currently does not bother trying to work out
  173.            curved paths accurately. """
  174.  
  175.         _logger.warning("LineKeyGtk should be using the implementation in KeyGtk")
  176.  
  177.         x = self.coordList[0]
  178.         y = self.coordList[1]
  179.         c = 2
  180.         coordLen = len(self.coordList)
  181.         within = False
  182.         
  183.         sMouseX = location[0] / scale[0]
  184.         sMouseY = location[1] / scale[1]
  185.         
  186.         while not c == coordLen:
  187.  
  188.             xp1 = self.coordList[c+1]
  189.             yp1 = self.coordList[c+2]
  190.             try:
  191.                 if self.coordList[c] == "L":
  192.                     within = (self.pointCrossesEdge(x,y,xp1,yp1,sMouseX,sMouseY) ^ within) # a xor        
  193.                     c +=3
  194.                     x = xp1
  195.                     y = yp1
  196.                         
  197.                 else:   
  198.                     xp2 = self.coordList[c+3]
  199.                     yp2 = self.coordList[c+4]
  200.                     xp3 = self.coordList[c+5]
  201.                     yp3 = self.coordList[c+6]
  202.                     within = (self.pointCrossesEdge(x,y,xp3,yp3,sMouseX,sMouseY) ^ within) # a xor 
  203.                     x = xp3
  204.                     y = yp3
  205.                     c += 7
  206.  
  207.             except ZeroDivisionError, (strerror):
  208.                 print strerror
  209.                 print "x: %f, y: %f, yp1: %f" % (x,y,yp1)
  210.         return within
  211.         
  212.     def paint(self, scale, context = None):
  213.         """ 
  214.         This class is quite hard to abstract, so all of its
  215.         processing lies now in the UI-dependent class. 
  216.         """
  217.                
  218.     def paint_font(self, scale):
  219.         KeyCommon.paint_font(self, scale, 
  220.             (self.coordList[0], self.coordList[1]))
  221.             
  222.     
  223.     
  224. class RectKeyCommon(KeyCommon):
  225.     """ An abstract class for rectangular keyboard buttons """
  226.  
  227.     name = None
  228.     """ Unique identifier for the key """
  229.  
  230.     location = None
  231.     """ Coordinates of the key on the keyboard """
  232.  
  233.     geometry = None
  234.     """ Width and height of the key """
  235.  
  236.     rgba = None
  237.     """ Colour of the key """
  238.  
  239.     def __init__(self, name, location, geometry, rgba):
  240.         KeyCommon.__init__(self)
  241.         self.name = name
  242.         self.location = location
  243.         self.geometry = geometry
  244.         self.rgba = rgba      
  245.       
  246.     def point_within_key(self, point_location, scale):
  247.         return  point_location[0] / scale[0] > self.location[0] \
  248.             and (point_location[0] / scale[0]
  249.                 < (self.location[0] + self.geometry[0])) \
  250.             and point_location[1] / scale[1] > self.location[1] \
  251.             and (point_location[1] / scale[1]
  252.                 < (self.location[1] + self.geometry[1]))
  253.     
  254.     def paint(self, scale, context = None):
  255.         pass
  256.